﻿Imports System.Xml.Serialization
Imports Framework.Data

Partial Public Class Manager
    Private Sub New()
    End Sub

    'Private Shared mMessageSubscriber As New MessageSubscriber

    Public Shared Sub Start()
        Framework.Net.Manager.RegisterCommand(New Commands.GetShares)
        Framework.Net.Manager.RegisterCommand(New Commands.GetShareItems)
        Framework.Net.Manager.RegisterCommand(New Commands.GetSharedItemFiles)
        Framework.Net.Manager.RegisterCommand(New Commands.GetSharedItemDetails)
        Framework.Net.Manager.RegisterCommand(New Commands.GetAvailableParts)
        Framework.Net.Manager.RegisterCommand(New Commands.RequestFilePart)
        Framework.Net.Manager.RegisterCommand(New Commands.NewAvailablePartsNotification)
        Framework.Net.Manager.RegisterCommand(New Commands.CancelFilePartRequest)

        'make sure security group for incoming files is setup
        If Settings.IncomingDownloadsGrantedToSecurityGroupIds.Count = 1 AndAlso Settings.IncomingDownloadsGrantedToSecurityGroupIds(0) = -1 Then
            Dim aNew As New Generic.HashSet(Of Integer)
            aNew.Add(Framework.Security.Manager.GetByTypeAndSubType(Entities.SecurityGroupType.System, Entities.SecurityGroupSubType.AuthenticatedUsers).Id)
            Using t As New TransactionScope
                Settings.SetIncomingDownloadsGrantedToSecurityGroupIds(aNew, t)
                t.Complete()
            End Using
        End If

        EntityHandlers.SharedItemsFiles.RemoveOldOrphans()

        FileSystemScan.RescanWorker.Start()
        TransferEngine.Manager.Start()
    End Sub

    Public Shared Sub [Stop](ByVal message As Framework.Message)
        message.SetMessage("Shutting down fileshare engine...")
        FileSystemScan.RescanWorker.Stop(message)
        TransferEngine.Manager.Stop(message)
    End Sub

    Public Shared Sub RescanShare(ByVal share As Entities.Share)
        RescanShare(share, TimeSpan.FromSeconds(30))
    End Sub
    Public Shared Sub RescanShare(ByVal share As Entities.Share, ByVal delay As TimeSpan)
        FileSystemScan.RescanQueue.Queue(share, Nothing, delay)
    End Sub


    Private Shared Function DoWeAlreadyHaveThisFile(ByVal sha512Hash As String, ByVal size As Long) As Boolean
        Dim clsFile As Entities.SharedItemFile = EntityHandlers.SharedItemsFiles.GetBySHA512HashAndSizeNotOrphaned(sha512Hash, size)
        Return clsFile IsNot Nothing
    End Function
    Private Shared Function AreWeAlreadyDownloadingThisFile(ByVal sha512Hash As String, ByVal size As Long) As Boolean
        Dim clsFile As Entities.Downloads.File = EntityHandlers.DownloadsFiles.GetBySHA512HashAndSize(sha512Hash, size)
        If clsFile IsNot Nothing AndAlso clsFile.Status = Entities.Downloads.Status.Deleted Then
            Return False
        ElseIf clsFile IsNot Nothing Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Sub DeleteShare(ByVal share As Entities.Share)
        EntityHandlers.Shares.Delete(share.Id)
    End Sub

    Public Shared Function GetAllItemFiles(ByVal node As Framework.Net.Node, ByVal sharedItemId As Integer) As Generic.List(Of Entities.SharedItemFile)
        Return Commands.GetSharedItemFiles.Execute(node, sharedItemId)
    End Function

    Public Shared Sub StartDownload(ByVal sharedItem As Commands.GetShareItems.ItemInfo, ByVal sharedFiles As Generic.List(Of Entities.SharedItemFile))
        TransferEngine.Manager.StartDownload(sharedItem, sharedFiles)
    End Sub
    Public Shared Sub RecycleDownloads(ByVal file As Entities.Downloads.File)
        TransferEngine.Manager.Recycle(file)
    End Sub
    Public Shared Sub RecycleDownloads(ByVal files As Generic.List(Of Entities.Downloads.File))
        TransferEngine.Manager.Recycle(files)
    End Sub
    Public Shared Sub DeleteDownloads(ByVal files As Generic.List(Of Entities.Downloads.File))
        TransferEngine.Manager.Delete(files)
    End Sub
    Public Shared Sub ResumeDownloads(ByVal files As Generic.List(Of Entities.Downloads.File))
        TransferEngine.Manager.Resume(files)
    End Sub
    Public Shared Sub PauseDownloads(ByVal files As Generic.List(Of Entities.Downloads.File))
        TransferEngine.Manager.Pause(files)
    End Sub
    Public Shared Function GetCurrentReadRatesByFileId() As Generic.Dictionary(Of Integer, Integer)
        Return TransferEngine.Manager.GetCurrentReadRatesByFileId()
    End Function

    Public Shared Sub UpdateMediaInfoForExistingItem(ByVal item As Entities.SharedItem)
        FileSystemScan.ScanFolder_Common.UpdateMediaInfoForExistingItem(item)
    End Sub
    Public Shared Sub UpdateMediaInfoForExistingItem(ByVal item As Entities.SharedItem, ByVal mode As CreateMediaInfoMode)
        FileSystemScan.ScanFolder_Common.UpdateMediaInfoForExistingItem(item, mode)
    End Sub
    Public Shared Sub UpdateMediaInfoForExistingItem(ByVal item As Entities.SharedItem, ByVal imdbHintUri As Uri, ByVal mode As CreateMediaInfoMode)
        FileSystemScan.ScanFolder_Common.UpdateMediaInfoForExistingItem(item, imdbHintUri, mode)
    End Sub
    Public Shared Function FindYearInComment(ByVal text As String) As String
        Return Handler_OpenMediaInfo.FindYearInComment(text)
    End Function

End Class

Public Enum CreateMediaInfoMode
    Normal
    TagAsFeatureNotAvailable
    ForceHintImdbUriAsFeature
End Enum
